package de.ars.client;

import java.io.IOException;
import java.util.Vector;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;

import de.ars.client.datahandling.DataMangementSystem;
import de.ars.client.datahandling.PropertyReader;
import de.ars.client.screens.LoginScreen;
import de.ars.client.screens.MainScreen;
import de.ars.client.screens.SplashScreen;
import de.ars.common.IDataModelConstants;

public class Main extends MIDlet
{
    private static final int SPLASH_TIME = 3000;

    private SplashScreen scr_Splash;
    private LoginScreen scr_Login;
    private MainScreen scr_Main;
    public Vector v_fonts = null;
    
    protected static Main instance = null;

    public Main()
    {
        instance = this;
    }
    
    public static Main getInstance()
    {
        return instance;
    }

    protected void destroyApp( boolean arg0 ) throws MIDletStateChangeException
    {
        // TODO Auto-generated method stub

    }

    protected void pauseApp()
    {
        // TODO Auto-generated method stub

    }

    protected void startApp() throws MIDletStateChangeException
    {
        Display.init( this );

        new PropertyReader( "/resources_en.properties" );

        scr_Splash = new SplashScreen( this, PropertyReader.getProperty( "screen.splash" ) );
        scr_Splash.show();

        new Thread()
        {
            public void run()
            {
                scr_Splash.setTaskText( "loading lwuit-skin" );
                Resources r = null;
                try
                {
                    r = Resources.open( "/default/theme.res" );
                }
                catch( IOException ex )
                {
                    ex.printStackTrace();
                }
                UIManager.getInstance().setThemeProps( r.getTheme( "Makeover" ) );
                
                scr_Splash.refreshTheme();
                getAllFonts( r );

                scr_Splash.setTaskText( PropertyReader.getProperty( "label.loadingScreens" ) );

                scr_Login = new LoginScreen( Main.this, PropertyReader.getProperty( "screen.login" ) );
                
                scr_Splash.setTaskText( PropertyReader.getProperty( "label.loadingInformationGroups" ) );
                
                scr_Main = new MainScreen( Main.this, DataMangementSystem.loadInformationGroup() );

                scr_Splash.setTaskText( PropertyReader.getProperty( "label.loadingSplash" ) );

                try
                {
                    Thread.sleep( SPLASH_TIME );
                }
                catch( InterruptedException e )
                {
                    e.printStackTrace();
                }

                Main.this.showScreen( IDataModelConstants.SCREEN_LOGIN );
                
                new Thread()
                {
                    public void run()
                    {
                        DataMangementSystem.loadPersonsFromFS( scr_Main ) ;
                    }
                }.start();
            }
        }.start();
    }

    public void showScreen( int index )
    {
        switch( index )
        {
            case IDataModelConstants.SCREEN_MAIN:
                scr_Main.show();
                break;
            case IDataModelConstants.SCREEN_LOGIN:
                scr_Login.show();
                break;
        }
    }
    
    private void getAllFonts( Resources r )
    {
        v_fonts = new Vector();
        String[] s_temp = r.getFontResourceNames();
        for( int i = 0; i < s_temp.length; i++ )
            v_fonts.addElement( r.getFont( s_temp[i] ) );
    }

    public Font getFont( int index )
    {
        if( index < v_fonts.size() && index >= 0 )
            return ((Font)v_fonts.elementAt( index ));
        else
            return null;
    }

}
